#ifndef __POLLER_H__
#define __POLLER_H__
#include "NonCopyable.h"
#include "Timestamp.h"
#include "EventLoop.h"
#include <vector>
#include <map>

using std::vector;
using std::map;
using muduo::Timestamp;

struct pollfd;
class Channel;
class Poller : NonCopyable
{
public:
    typedef vector<Channel*> ChannelList;

    Poller(EventLoop *loop);
    ~Poller();

    // polls IO events
    // must be called in loop thread
    Timestamp poll(int timeoutMs, ChannelList *activeChannels);

    // change the interested IO events
    // must be called in loop thread
    void updateChannel(Channel *channel); //根据channel更新_pollfds
    void removeChannel(Channel *channel); //从_pollfds中删除channel

    void assertInLoopThread() { _ownerLoop->assertInLoopThread(); }

private:
    // 遍历_pollfds 找出有活动事件的fd 将对应的Channel填入activeChannels
    void fillActiveChannels(int eventNum, ChannelList *activeChannels) const;

    typedef vector<struct pollfd> PollFdList;
    typedef map<int, Channel*> ChannelMap; // fd->channel映射
    
    EventLoop *_ownerLoop;
    PollFdList _pollfds;
    ChannelMap _channels;
};

#endif